<html>
<head><meta charset="utf-8"><title>js-rust comparisons · wg-async-foundations · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/index.html">wg-async-foundations</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html">js-rust comparisons</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="246256392"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246256392" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chaz Kiker <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246256392">(Jul 16 2021 at 17:47)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/187312-wg-async-foundations/topic/meeting.202021-07-16/near/246256270">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="419100">Chaz Kiker</span> that would be awesome!</p>
</blockquote>
<p>where's the place to do this? make a PR to <code>rust-lang/wg-async-foundations</code>? shoot you a link to a HackMD file? just let me know!</p>



<a name="246256447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246256447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chaz Kiker <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246256447">(Jul 16 2021 at 17:48)</a>:</h4>
<p>and thanks everyone for the meeting today and all the work you do for Rust :)</p>



<a name="246256638"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246256638" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246256638">(Jul 16 2021 at 17:49)</a>:</h4>
<p>HackMD is a good place for drafting. Do you think it could potentially go in <a href="https://rust-lang.github.io/async-book/01_getting_started/01_chapter.html">the async book</a>?</p>



<a name="246256854"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246256854" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chaz Kiker <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246256854">(Jul 16 2021 at 17:51)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116883">tmandry</span> <a href="#narrow/stream/187312-wg-async-foundations/topic/meeting.202021-07-16/near/246256638">said</a>:</p>
<blockquote>
<p>HackMD is a good place for drafting. Do you think it could potentially go in <a href="https://rust-lang.github.io/async-book/01_getting_started/01_chapter.html">the async book</a>?</p>
</blockquote>
<p>good question! I guess I'm not totally sure!  </p>
<p>in some ways I suppose it'd be weird to toss JS examples in a Rust book... but on the other hand I feel like so many people are familiar with the JS Promise that it may be acceptable. </p>
<p>Maybe at least a mention of it and a link to a separate resource that shows it?</p>
<p>But what do you think? I'd certainly defer to y'all!</p>



<a name="246257030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246257030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246257030">(Jul 16 2021 at 17:52)</a>:</h4>
<p>I could see the book containing comparisons with many languages, not just JS</p>



<a name="246257046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246257046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246257046">(Jul 16 2021 at 17:52)</a>:</h4>
<p>but it would also be fine to host it separately and link to it</p>



<a name="246257092"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246257092" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246257092">(Jul 16 2021 at 17:53)</a>:</h4>
<p>whatever you do, drop a link here when you'd like a review :)</p>



<a name="246257101"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246257101" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ibraheem Ahmed <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246257101">(Jul 16 2021 at 17:53)</a>:</h4>
<p>Maybe some basic comparisons, but I think it would be better separate</p>



<a name="246257122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246257122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ibraheem Ahmed <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246257122">(Jul 16 2021 at 17:53)</a>:</h4>
<p>And the async book could have a collection of links to other learning material</p>



<a name="246257135"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246257135" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ibraheem Ahmed <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246257135">(Jul 16 2021 at 17:53)</a>:</h4>
<p>It can't cover everything on it's own</p>



<a name="246257152"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246257152" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246257152">(Jul 16 2021 at 17:53)</a>:</h4>
<p>if it's more than one page then yeah I agree that it should be separate</p>



<a name="246257224"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246257224" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chaz Kiker <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246257224">(Jul 16 2021 at 17:54)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116883">tmandry</span> <a href="#narrow/stream/187312-wg-async-foundations/topic/meeting.202021-07-16/near/246257092">said</a>:</p>
<blockquote>
<p>whatever you do, drop a link here when you'd like a review :)</p>
</blockquote>
<p>okay will do. and it'll probably be easier to decide on where it should exist once we take a look at it!</p>



<a name="246257232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246257232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Doc Jones <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246257232">(Jul 16 2021 at 17:54)</a>:</h4>
<p>I find an abundance of writing about this topic covering various languages in blog posts via google.  Maybe not ideal, but many people have written about it.</p>



<a name="246257335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246257335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246257335">(Jul 16 2021 at 17:55)</a>:</h4>
<p>good to know. blog posts are useful but also not the same as documentation that gets revised over time. I can see there being a place for both.</p>



<a name="246257354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246257354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Doc Jones <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246257354">(Jul 16 2021 at 17:55)</a>:</h4>
<p>My favorite is a post about working all of the Oracle Java tutorial problems in Rust.</p>



<a name="246257474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246257474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chaz Kiker <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246257474">(Jul 16 2021 at 17:56)</a>:</h4>
<p><span class="user-mention silent" data-user-id="394977">Doc Jones</span> <a href="#narrow/stream/187312-wg-async-foundations/topic/meeting.202021-07-16/near/246257232">said</a>:</p>
<blockquote>
<p>I find an abundance of writing about this topic covering various languages in blog posts via google.  Maybe not ideal, but many people have written about it.</p>
</blockquote>
<p>Yes... but there are still niche cases that I never found covered. It's all the more basic/standard examples.</p>



<a name="246257619"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246257619" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Doc Jones <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246257619">(Jul 16 2021 at 17:57)</a>:</h4>
<p>I'm not arguing against doing it.  I'm pointing out that the pain is real and examples exist.</p>



<a name="246257904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246257904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246257904">(Jul 16 2021 at 17:59)</a>:</h4>
<p>I broke this out into a distinct topic</p>



<a name="246257923"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246257923" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246257923">(Jul 16 2021 at 17:59)</a>:</h4>
<p>one thing though -- there is also <a class="stream" data-stream-id="201246" href="/#narrow/stream/201246-wg-async-foundations.2Fbook">#wg-async-foundations/book</a></p>



<a name="246258114"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246258114" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chaz Kiker <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246258114">(Jul 16 2021 at 18:00)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/187312-wg-async-foundations/topic/js-rust.20comparisons/near/246257923">said</a>:</p>
<blockquote>
<p>one thing though -- there is also <a class="stream" data-stream-id="201246" href="/#narrow/stream/201246-wg-async-foundations.2Fbook">#wg-async-foundations/book</a></p>
</blockquote>
<p>ah yes, maybe this would fit better there?</p>



<a name="246297846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246297846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chaz Kiker <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246297846">(Jul 17 2021 at 01:56)</a>:</h4>
<p>got caught up with some other stuff today, but <a href="https://hackmd.io/@chazkiker/compare-async-rust-to-js">here's an initial draft</a> with practically every possible way to make a Promise in JS. </p>
<p>I could use some help with the mirrored Rust code... I honestly am unsure what the idiomatic way to write ones own futures are... also happy to figure it out if anyone's got resources they think might be worth sharing.</p>



<a name="246298459"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246298459" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zoey <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246298459">(Jul 17 2021 at 02:11)</a>:</h4>
<p><a href="https://fasterthanli.me/articles/pin-and-suffering">https://fasterthanli.me/articles/pin-and-suffering</a> might help you, it shows how to impl a basic future by hand</p>



<a name="246299664"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246299664" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chaz Kiker <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246299664">(Jul 17 2021 at 02:39)</a>:</h4>
<p><span class="user-mention" data-user-id="398288">@Zoey</span> that is immensely helpful — thanks for the resource.</p>



<a name="246299673"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246299673" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zoey <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246299673">(Jul 17 2021 at 02:39)</a>:</h4>
<p>If you have any not-worth-asking-groups-questions, feel free to PM them to me</p>



<a name="246315989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246315989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246315989">(Jul 17 2021 at 09:44)</a>:</h4>
<p>I do not think that manually implementing <code>Future</code> is equivalent to <code>new Promise</code>. As I see it, the actual equivalent is an oneshot channel.</p>



<a name="246319242"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246319242" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chaz Kiker <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246319242">(Jul 17 2021 at 11:07)</a>:</h4>
<p><span class="user-mention" data-user-id="218683">@Alice Ryhl</span> ohhh… interesting. yes I could see that. </p>
<p>I am no expert in this realm, so i’d defer to y’all… but <a href="https://docs.rs/tokio/1.8.1/tokio/sync/oneshot/index.html">this api</a> does look rather JS Promise-y… </p>
<p>(this little pursuit has taught me just how little I know about Rust Futures and async runtime… they are a different beast than JS no doubt.)</p>



<a name="246319299"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246319299" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chaz Kiker <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246319299">(Jul 17 2021 at 11:09)</a>:</h4>
<p><span class="user-mention" data-user-id="218683">@Alice Ryhl</span> any chance you’d be willing to spin up a quick example for what you think the Rusty <code>new Promise</code> equivalent would look like?</p>



<a name="246319362"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246319362" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246319362">(Jul 17 2021 at 11:10)</a>:</h4>
<p>This example?</p>
<div class="codehilite" data-code-language="JavaScript"><pre><span></span><code><span class="c1">// JavaScript</span>
<span class="k">async</span> <span class="kd">function</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
  <span class="c1">// Promise { 42 }</span>
  <span class="kd">const</span> <span class="nx">promise</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Promise</span><span class="p">((</span><span class="nx">resolve</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="nx">resolve</span><span class="p">(</span><span class="mf">42</span><span class="p">));</span>
  <span class="c1">// 42</span>
  <span class="kd">const</span> <span class="nx">resolved_val</span> <span class="o">=</span> <span class="k">await</span> <span class="nx">promise</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>



<a name="246319370"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246319370" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246319370">(Jul 17 2021 at 11:10)</a>:</h4>
<p>When does the contents of the promise constructor run?</p>



<a name="246319375"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246319375" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246319375">(Jul 17 2021 at 11:10)</a>:</h4>
<p>Immediately, or as if you had used <code>setTimeout</code>?</p>



<a name="246319382"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246319382" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246319382">(Jul 17 2021 at 11:11)</a>:</h4>
<p>That is, <code>setTimeout</code> with no deadline / deadline of 0</p>



<a name="246319514"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246319514" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246319514">(Jul 17 2021 at 11:13)</a>:</h4>
<p>I guess the equivalent is something like this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">tokio</span>::<span class="n">sync</span>::<span class="n">oneshot</span><span class="p">;</span><span class="w"></span>

<span class="cp">#[tokio::main]</span><span class="w"></span>
<span class="k">async</span><span class="w"> </span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">resolve</span><span class="p">,</span><span class="w"> </span><span class="n">promise</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">oneshot</span>::<span class="n">channel</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="n">tokio</span>::<span class="n">spawn</span><span class="p">(</span><span class="k">async</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">resolve</span><span class="p">.</span><span class="n">send</span><span class="p">(</span><span class="mi">42</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">});</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">resolved_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">promise</span><span class="p">.</span><span class="k">await</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="246319566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246319566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246319566">(Jul 17 2021 at 11:14)</a>:</h4>
<p>Hmm, it seems like the actual equivalent wouldn't be spawning a task here</p>



<a name="246319597"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246319597" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246319597">(Jul 17 2021 at 11:15)</a>:</h4>
<p>anyways, <code>then</code> is equivalent to spawning a task that awaits the oneshot receiver, then runs the content of the <code>then</code> closure</p>



<a name="246319600"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246319600" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chaz Kiker <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246319600">(Jul 17 2021 at 11:15)</a>:</h4>
<p>I believe the call to <code>new Promise</code> will toss the callback passed in to the constructor onto the event queue. </p>
<p>so that code will execute and essentially we have a <code>Promise</code> that immediately resolves itself and is ready to be <code>await</code>-ed. </p>
<p>the  call to <code>await</code> will just take the resolved value.</p>



<a name="246319611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246319611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246319611">(Jul 17 2021 at 11:16)</a>:</h4>
<p>Well I tried this:</p>
<div class="codehilite" data-code-language="JavaScript"><pre><span></span><code><span class="kd">const</span> <span class="nx">promise</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Promise</span><span class="p">((</span><span class="nx">resolve</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"A"</span><span class="p">));</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"B"</span><span class="p">);</span>
</code></pre></div>
<p>It prints <code>A</code> first</p>



<a name="246319669"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246319669" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246319669">(Jul 17 2021 at 11:16)</a>:</h4>
<p>So it doesn't go on the event queue, hence no spawn</p>



<a name="246319671"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246319671" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246319671">(Jul 17 2021 at 11:16)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">tokio</span>::<span class="n">sync</span>::<span class="n">oneshot</span><span class="p">;</span><span class="w"></span>

<span class="cp">#[tokio::main]</span><span class="w"></span>
<span class="k">async</span><span class="w"> </span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">resolve</span><span class="p">,</span><span class="w"> </span><span class="n">promise</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">oneshot</span>::<span class="n">channel</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">resolve</span><span class="p">.</span><span class="n">send</span><span class="p">(</span><span class="mi">42</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">resolved_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">promise</span><span class="p">.</span><span class="k">await</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="246319721"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246319721" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246319721">(Jul 17 2021 at 11:18)</a>:</h4>
<p>Chaining <code>then</code> is equivalent to having a single function that just has all the contents of each <code>then</code> in sequence with an <code>.await</code> at each <code>then</code></p>



<a name="246319760"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246319760" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chaz Kiker <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246319760">(Jul 17 2021 at 11:19)</a>:</h4>
<p>ah, yes, okay. here's <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#timing">MDN's "Timing" section</a>. </p>
<p>I think you are totally 100% correct on all fronts here.</p>



<a name="246319799"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246319799" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246319799">(Jul 17 2021 at 11:19)</a>:</h4>
<p>e.g. this:</p>
<div class="codehilite" data-code-language="JavaScript"><pre><span></span><code><span class="nx">myPromise</span>
    <span class="p">.</span><span class="nx">then</span><span class="p">(</span><span class="nx">handleResolvedA</span><span class="p">)</span>
    <span class="p">.</span><span class="nx">then</span><span class="p">(</span><span class="nx">handleResolvedB</span><span class="p">)</span>
    <span class="p">.</span><span class="nx">then</span><span class="p">(</span><span class="nx">handleResolvedC</span><span class="p">);</span>
</code></pre></div>
<p>becomes</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">tokio</span>::<span class="n">spawn</span><span class="p">(</span><span class="k">async</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">myPromise</span><span class="p">.</span><span class="k">await</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">handleResolvedA</span><span class="p">(</span><span class="n">value</span><span class="p">).</span><span class="k">await</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">handleResolvedB</span><span class="p">(</span><span class="n">value</span><span class="p">).</span><span class="k">await</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">handleResolvedC</span><span class="p">(</span><span class="n">value</span><span class="p">).</span><span class="k">await</span><span class="p">;</span><span class="w"></span>
<span class="p">});</span><span class="w"></span>
</code></pre></div>



<a name="246320010"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246320010" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chaz Kiker <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246320010">(Jul 17 2021 at 11:24)</a>:</h4>
<p>okay — and what do you think RE: <code>.catch()</code>?? </p>
<p>If we want to replicate </p>
<div class="codehilite" data-code-language="JavaScript"><pre><span></span><code><span class="k">new</span> <span class="nb">Promise</span><span class="p">((</span><span class="nx">_</span><span class="p">,</span> <span class="nx">reject</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="nx">reject</span><span class="p">(</span><span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s2">"Error"</span><span class="p">)))</span>
</code></pre></div>
<p>would we just...</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">tokio</span>::<span class="n">sync</span>::<span class="n">oneshot</span><span class="p">;</span><span class="w"></span>

<span class="cp">#[tokio::main]</span><span class="w"></span>
<span class="k">async</span><span class="w"> </span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">resolve</span><span class="p">,</span><span class="w"> </span><span class="n">promise</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">oneshot</span>::<span class="n">channel</span>::<span class="o">&lt;</span><span class="nb">Result</span><span class="o">&lt;</span><span class="kt">u32</span><span class="p">,</span><span class="w"> </span><span class="nb">Box</span><span class="o">&lt;</span><span class="k">dyn</span><span class="w"> </span><span class="n">Error</span><span class="o">&gt;&gt;&gt;</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">resolve</span><span class="p">.</span><span class="n">send</span><span class="p">(</span><span class="nb">Err</span><span class="p">(</span><span class="n">some_err</span><span class="p">));</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">rejected_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">promise</span><span class="p">.</span><span class="k">await</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="246320047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246320047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246320047">(Jul 17 2021 at 11:25)</a>:</h4>
<p>Yes, Rust futures do not have built-in support for the output being failure, so you need to use a <code>Result</code> as message type to handle that</p>



<a name="246320107"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246320107" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246320107">(Jul 17 2021 at 11:26)</a>:</h4>
<p>It's probably worth being clear that in Rust, you use oneshot channels more rarely than promises are used in JS</p>



<a name="246320108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246320108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chaz Kiker <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246320108">(Jul 17 2021 at 11:26)</a>:</h4>
<p>Okay sweet. Thanks for the callout. I'll change that example to use <code>oneshot::channel</code> tomorrow and give the other examples a try.</p>
<p>Appreciate the insight <span aria-label="smiley" class="emoji emoji-1f603" role="img" title="smiley">:smiley:</span></p>



<a name="246320262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246320262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chaz Kiker <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246320262">(Jul 17 2021 at 11:30)</a>:</h4>
<p><span class="user-mention silent" data-user-id="218683">Alice Ryhl</span> <a href="#narrow/stream/187312-wg-async-foundations/topic/js-rust.20comparisons/near/246320107">said</a>:</p>
<blockquote>
<p>It's probably worth being clear that in Rust, you use oneshot channels more rarely than promises are used in JS</p>
</blockquote>
<p>Yes, that's a fair point... I guess that's a whole different challenge to this problem... <code>Rust != JS</code>... so you <em>can</em> use a oneshot but maybe <em>don't</em> default to it?  </p>
<p>Couple of things making me re-consider the value of this JS vs Rust comparison.</p>



<a name="246322903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246322903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246322903">(Jul 17 2021 at 12:37)</a>:</h4>
<p>I mean, the reason is that you can just, you know, await the thing</p>



<a name="246322911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246322911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246322911">(Jul 17 2021 at 12:37)</a>:</h4>
<p>If you want it to run separately, you spawn a task, then await it there</p>



<a name="246322914"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246322914" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246322914">(Jul 17 2021 at 12:37)</a>:</h4>
<p>you don't use a promise for that</p>



<a name="246329156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246329156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ibraheem Ahmed <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246329156">(Jul 17 2021 at 15:09)</a>:</h4>
<p><span class="user-mention silent" data-user-id="419100">Chaz Kiker</span> <a href="#narrow/stream/187312-wg-async-foundations/topic/js-rust.20comparisons/near/246320010">said</a>:</p>
<blockquote>
<p>okay — and what do you think RE: <code>.catch()</code>?? </p>
</blockquote>
<p>Probably <code>map_err</code> or <code>or_else</code> from <a href="https://docs.rs/futures/0.3.15/futures/future/trait.TryFutureExt.html"><code>TryFutureExt</code></a>.</p>



<a name="246329220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246329220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246329220">(Jul 17 2021 at 15:10)</a>:</h4>
<p>I mean, you just use ordinary Rust error handling</p>



<a name="246329221"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246329221" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246329221">(Jul 17 2021 at 15:10)</a>:</h4>
<p>maching</p>



<a name="246329223"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246329223" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246329223">(Jul 17 2021 at 15:10)</a>:</h4>
<p>question marks</p>



<a name="246329225"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246329225" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246329225">(Jul 17 2021 at 15:10)</a>:</h4>
<p>that kinda stuff</p>



<a name="246329233"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246329233" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246329233">(Jul 17 2021 at 15:11)</a>:</h4>
<p>There isn't any need for fancy combinators tbh</p>



<a name="246331087"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246331087" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ibraheem Ahmed <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246331087">(Jul 17 2021 at 15:56)</a>:</h4>
<p>Well yes but those would be the closest in syntax.</p>



<a name="246332992"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246332992" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthias247 <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246332992">(Jul 17 2021 at 16:40)</a>:</h4>
<p><span class="user-mention silent" data-user-id="218683">Alice Ryhl</span> <a href="#narrow/stream/187312-wg-async-foundations/topic/js-rust.20comparisons/near/246319799">said</a>:</p>
<blockquote>
<p>e.g. this:</p>
<div class="codehilite" data-code-language="JavaScript"><pre><span></span><code><span class="nx">myPromise</span>
    <span class="p">.</span><span class="nx">then</span><span class="p">(</span><span class="nx">handleResolvedA</span><span class="p">)</span>
    <span class="p">.</span><span class="nx">then</span><span class="p">(</span><span class="nx">handleResolvedB</span><span class="p">)</span>
    <span class="p">.</span><span class="nx">then</span><span class="p">(</span><span class="nx">handleResolvedC</span><span class="p">);</span>
</code></pre></div>
<p>becomes</p>
<p><div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">tokio</span>::<span class="n">spawn</span><span class="p">(</span><span class="k">async</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">myPromise</span><span class="p">.</span><span class="k">await</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">handleResolvedA</span><span class="p">(</span><span class="n">value</span><span class="p">).</span><span class="k">await</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">handleResolvedB</span><span class="p">(</span><span class="n">value</span><span class="p">).</span><span class="k">await</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">handleResolvedC</span><span class="p">(</span><span class="n">value</span><span class="p">).</span><span class="k">await</span><span class="p">;</span><span class="w"></span>
<span class="p">});</span><span class="w"></span>
</code></pre></div><br>
</p>
</blockquote>
<p>close - you would need to insert a <code>tokio::task:::yield_now().await</code> after each Rust line - since Javascript will invoke continuations in a new eventloop iteration.</p>



<a name="246336101"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/js-rust%20comparisons/near/246336101" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/js-rust.20comparisons.html#246336101">(Jul 17 2021 at 18:02)</a>:</h4>
<p>fair enough</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>